package org.sunsys.spitterapp.controller;


import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.sunsys.spitterapp.Spitter;

@Controller
public class SpitterFormController {

	
	public SpitterFormController() {
	}
	
	
	@RequestMapping(value="/signupForm", method=RequestMethod.GET, params="new")
	public String showForm(Model model) {
		model.addAttribute(new Spitter());
		System.out.println("showForm method");
		
		return "redirect:/SignupForm.html";
	}
	
	@RequestMapping(value="/singupForm", method={RequestMethod.GET, RequestMethod.POST}, params="signup")
	public String handleFormSubmition(@Valid Spitter spitter, BindingResult bindingResult){
		System.out.println("Name:"+spitter.getUserName());
		System.out.println("Passwrod:"+spitter.getPassword());
		System.out.println("Full Name:"+spitter.getFullName());
		
		if(bindingResult.hasErrors()) {
			return "forward:/ErrorPage.html";
		}
		
		System.out.println("handle Form Submittion");
		return "forward:/SignupForm.html";
	}
	
}
